﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.Linq;

namespace _7.ParsingMatchInfo
{
    class ImportMatch
    {
        class GameInfo
        {
            public int GameID { get; set; }
            public string HomeTeamName { get; set; }
            public string AwayTeamName { get; set; }
            public Dictionary<int, string> HomePlayers;
            public Dictionary<int, string> AwayPlayers;

            public GameInfo(int id, string home, string away,
                Dictionary<int, string> homeplayers, Dictionary<int, string> awayplayers)
            {
                this.GameID = id;
                this.HomeTeamName = home;
                this.AwayTeamName = away;
                this.HomePlayers = homeplayers;
                this.AwayPlayers = awayplayers;
            }

        }

        static List<GameInfo> GetMatchInfoFromXML()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("../../Games.xml");

            XmlNode rootNode = doc.DocumentElement;
            List<GameInfo> allGames = new List<GameInfo>();

            foreach (XmlNode game in rootNode.ChildNodes)
            {
                int id = int.Parse(game.Attributes[0].Value);

                Dictionary<int, string> homeplayers = new Dictionary<int, string>();
                string hometeamName = game["home-team"].Attributes[0].Value;
                foreach (XmlNode player in game["home-team"].ChildNodes)
                {
                    int playerNumber = int.Parse(player.Attributes[0].Value);
                    string playerName = player.Attributes[1].Value;

                    homeplayers.Add(playerNumber, playerName);
                }

                Dictionary<int, string> awayplayers = new Dictionary<int, string>();
                string awayteamName = game["away-team"].Attributes[0].Value;
                foreach (XmlNode player in game["away-team"].ChildNodes)
                {
                    int playerNumber = int.Parse(player.Attributes[0].Value);
                    string playerName = player.Attributes[1].Value;

                    awayplayers.Add(playerNumber, playerName);
                }

                GameInfo currGame = new GameInfo(id, hometeamName, awayteamName, homeplayers, awayplayers);
                allGames.Add(currGame);
            }

            return allGames;
        }

        static void ImportInDBSchema()
        {
            FootballDataContext context = new FootballDataContext();
            List<GameInfo> allGames = GetMatchInfoFromXML();

            foreach (GameInfo item in allGames)
            {
                foreach (var player in item.HomePlayers)
	            {
                    MatchInfo mInfo = new MatchInfo()
                    {
                        MatchID = item.GameID,
                        TeamName = item.HomeTeamName,
                        PlayerName = player.Value,
                        PlayerNum = player.Key
                    };
                    context.MatchInfos.InsertOnSubmit(mInfo);
                    context.SubmitChanges();

	            }
                foreach (var player in item.AwayPlayers)
                {
                    MatchInfo mInfo = new MatchInfo()
                    {
                        MatchID = item.GameID,
                        TeamName = item.AwayTeamName,
                        PlayerName = player.Value,
                        PlayerNum = player.Key
                    };
                    context.MatchInfos.InsertOnSubmit(mInfo);
                    context.SubmitChanges();
                }
               
            }
        }

        static void GetSomeInfo()
        {
            FootballDataContext context = new FootballDataContext();

            var MatchInfos = context.ExecuteQuery<MatchInfo>(
                "select * from MatchInfo");

            foreach (var item in MatchInfos)
            {
                Console.WriteLine(item.MatchID +" "+item.PlayerName + " "+item.PlayerNum + " "+item.TeamName);
            }

        }

        static void Main(string[] args)
        {
            //ImportInDBSchema();
            GetSomeInfo();
        }
    }
}

